package com.zl_huang.example.web;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zl_huang.example.model.User;
import com.zl_huang.example.service.IUserService;


@Controller
public class UserController {
	
	Map<String ,User> mapUsers = new HashMap<String , User>();
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private LocaleResolver localeResolver;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping("/users")
	public String users(Model model){
		List<User> list;
		try {
			list = userService.findAllUsers();			
			if(!list.isEmpty()){
				for(User user : list){
				    mapUsers.put(user.getUser_id(), user);
				}
			}
			model.addAttribute("edit", messageSource.getMessage("page.index.edit", null,LocaleContextHolder.getLocale()));
			model.addAttribute("userList", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "user/UserList";
	}
	
	@RequestMapping(value="/addUser" , method = RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("user", new User());
		return "user/add";
	}
	
	@RequestMapping(value="/addUser" , method = RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes , @Validated User user, BindingResult result){
		
		try {
			if(mapUsers.containsKey(user.getUser_id())){
				redirectAttributes.addFlashAttribute("message", "USER_ID已经存在，请重新添加");
				return "redirect:/addUser";
			}else{
				userService.createUser(user);
				redirectAttributes.addFlashAttribute("message", "添加成功");
				return "redirect:/users";
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
	}
	
	@RequestMapping(value = "/editUser/{user_id}" , method = RequestMethod.GET)
	public String toEdit(Model model , @PathVariable String user_id ){
		User user;
		try {
			user = userService.getUserById(user_id);
			model.addAttribute("user" , user );
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "user/edit";
	}
	
	@RequestMapping(value="/editUser/{user_id}" , method=RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes , User user , @PathVariable String user_id){
		
		
		try {
			user.setUser_id(user_id);
			userService.editUser(user);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/users";		
	}
	@RequestMapping(value="/deleteUser/{user_id}" , method=RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes , @PathVariable String user_id){
		try {
			userService.deleteUser(user_id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/users";
	}
	@RequestMapping(value="/changeLocale")
	public String changeLocale(HttpServletRequest request , HttpServletResponse response , String localeType){
		if("en_US".equals(localeType)){ 
			localeResolver.setLocale(request, response, Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}
		return "redirect:/users";
	}
	
	

}
